using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using AlumnoEjemplos.TP3D.Collider;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace AlumnoEjemplos.TP3D
{
    class Box : Plataforma
    {
        private Vector3 _size;
        private Mesh _mesh;
        private Texture _texture;
        private Vector3 _position;
        public bool _isMovingRight;
        public float tope = 300;
        protected ICollection<Placeable> _guests = new List<Placeable>();

        public Box(Mesh newBox, Texture texture, Vector3 position, float tope, Vector3 size)
        {
            this.Mesh = newBox;
            this.Texture = texture;
            this.Position = position;
            this.tope = tope;
            this._size = size;
        }

        public Vector3 Position
        {
            get {
                return _position;
            }
            set {
                _position = value;
            }
        }

        public Vector3 Size
        {
            get
            {
                return _size;
            }
            set
            {
                _size = value;
            }
        }

        protected Texture Texture
        {
            get {
                return _texture;
            }
            set {
                _texture = value;
            }
        }

        protected Mesh Mesh
        {
            get {
                return _mesh;
            }
            set {
                _mesh = value;
            }
        }

        internal void Render()
        {
            Device d3dDevice = TgcViewer.GuiController.Instance.D3dDevice;
            d3dDevice.SetTexture(0, this.Texture);
            d3dDevice.Transform.World = Matrix.Identity * Matrix.Translation(this.Position);
            this.Mesh.DrawSubset(0);
        }

        internal static Box CreateBox(Device d3dDevice, Texture texture, Vector3 size, Vector3 position, float tope)
        {
            Mesh newBox = Mesh.Box(d3dDevice, size.X, size.Y, size.Z);
            newBox = newBox.Clone(newBox.Options.Value,
                            VertexFormats.Position | VertexFormats.Normal | VertexFormats.Texture0 |
                            VertexFormats.Texture1, newBox.Device);
            using (VertexBuffer vb = newBox.VertexBuffer)
            {

                CustomVertex.PositionNormalTextured[] verts =
                    (CustomVertex.PositionNormalTextured[]) vb.Lock(0,
                                                                    typeof (CustomVertex.PositionNormalTextured),
                                                                    LockFlags.None,
                                                                    newBox.NumberVertices);
                try
                {
                    for (int i = 0; i < verts.Length; i += 4)
                    {
                        verts[i + 0].Tu = 0.0f;
                        verts[i + 0].Tv = 0.0f;
                        verts[i + 1].Tu = 1.0f;
                        verts[i + 1].Tv = 0.0f;
                        verts[i + 2].Tu = 1.0f;
                        verts[i + 2].Tv = 1.0f;
                        verts[i + 3].Tu = 0.0f;
                        verts[i + 3].Tv = 1.0f;
                    }
                }
                finally
                {
                    vb.Unlock();
                }
            }

            return new Box(newBox, texture, position, tope, size);
        }

        public void MoveX(float ammount)
        {
            Vector3 newPosition = this.Position + new Vector3(ammount, 0, 0);
            foreach (Placeable placeable in _guests)
            {
                placeable.Position = (placeable.Position - this.Position) + newPosition;
            }
            this.Position = newPosition;
        }

        public void doSomethingToMassive(Placeable p)
        {
        }

        #region Plataforma Members

        public void AddGuest(AlumnoEjemplos.TP3D.Collider.Placeable mario)
        {
            this._guests.Add(mario);
        }

        public Vector3 MovimientoSegun(Vector3 velocidad, Vector3 posicion)
        {
            return velocidad;
        }

        public float GetAltura(Vector3 position)
        {
            return this.Position.Y;
        }

        public void RemoveGuest(Mario mario)
        {
            this._guests.Remove(mario);
        }

        #endregion

        #region Plataforma Members


        public void Dispose()
        {
            this._texture.Dispose();
            this._mesh.Dispose();
        }

        #endregion

        #region Plataforma Members


        public bool EstasEn(float x, float z)
        {
            return x >= (this.Position.X - this.Size.X/2) && x <= (this.Position.X + this.Size.X/2)
                   && z >= (this.Position.Z - this.Size.Z/2) && z <= (this.Position.Z + this.Size.Z/2);
        }

        #endregion
    }
}
